package com.gidgabs.modules.api.service.impl;

import com.gidgabs.common.utils.*;
import com.gidgabs.config.GidgabsContants;
import com.gidgabs.dto.TbButlerOrderDto;
import com.gidgabs.modules.api.service.TbButlerOrderService;
import com.gidgabs.modules.dao.TbButlerOrderDao;
import com.gidgabs.modules.dao.TbUserIncomeDao;
import com.gidgabs.modules.entity.TbButlerOrderEntity;
import com.gidgabs.modules.entity.TbProductEntity;
import com.gidgabs.modules.entity.TbUserIncomeEntity;
import com.gidgabs.utils.SessionDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TbButlerOrderServiceImpl implements TbButlerOrderService {
    @Autowired
    private TbButlerOrderDao butlerOrderDao;

    @Autowired
    private PaypalUtil paypalUtil;

    @Autowired
    private TbUserIncomeDao tbUserIncomeDao;


    @Override
    public TbButlerOrderEntity queryObject(Integer id) {
        return butlerOrderDao.queryObject(id);
    }

    @Override
    public List<TbButlerOrderEntity> queryList(Map<String, Object> map) {
        return butlerOrderDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return butlerOrderDao.queryTotal(map);
    }

    @Override
    public void save(TbButlerOrderEntity butlerOrder) {
        butlerOrderDao.save(butlerOrder);
    }

    @Override
    public void update(TbButlerOrderEntity butlerOrder) {
        butlerOrderDao.update(butlerOrder);
        //添加收入明细
        TbUserIncomeEntity tbUserIncomeEntity = new TbUserIncomeEntity();
        tbUserIncomeEntity.setRevenueType(TbUserIncomeEntity.REVENUE_TYPE_RUN_LGE);
        tbUserIncomeEntity.setCreateTime(new Date());
        tbUserIncomeEntity.setDepict("这是跑腿收入明细");
        tbUserIncomeEntity.setLinkId(butlerOrder.getId());
        //收入=总价+小费
        tbUserIncomeEntity.setPrice(butlerOrder.getOrderPrice()+butlerOrder.getTip());
        tbUserIncomeEntity.setUserId(SessionDataUtil.getCurrentUserId());
        tbUserIncomeDao.save(tbUserIncomeEntity);
    }

    @Override
    public void delete(Integer id) {
        butlerOrderDao.delete(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        butlerOrderDao.deleteBatch(ids);
    }

    @Override
    public ReturnJson<TbButlerOrderDto> selectOrderList(Integer pageSize, Integer pageNo, Integer orderStatus, Integer selectType) {
        Assist assist = null;
        //如果不是查询所有的 就添加用户id
        if (selectType != 2) {
            assist = new Assist(
                    Assist.whereRequire("and (source_user_id = ", SessionDataUtil.getCurrentUserId()),
                    Assist.whereRequire("or user_id = ", SessionDataUtil.getCurrentUserId(), ")"));
        }else{
            assist = new Assist();
        }
        //根据创建时间排序
        Assist.WhereOrder createTime = Assist.order("create_time", false);
        assist.andEq("delete_type", DeleteType.NOT_DELETE.getKey())
                .andEq("order_status", orderStatus);
        assist.setOrder(createTime);
        assist.setRowSize(pageSize);
        assist.setStartRow(LangUtil.firstLimit(pageSize, pageNo));
        List<TbButlerOrderEntity> tbProductEntityList = butlerOrderDao.selectTbButlerOrder(assist);
        long count = butlerOrderDao.getTbButlerOrderRowCount(assist);
        List<TbButlerOrderDto> tbButlerOrderDtoList = new ArrayList<>();
        for (int i = 0; i < tbProductEntityList.size(); i++) {
            tbButlerOrderDtoList.add(tbProductEntityList.get(i).toDto());
        }
        return ReturnJson.success("查询成功", tbButlerOrderDtoList, count, pageSize, pageNo);
    }

    @Override
    public ReturnJson<String> payOrder(String orderCode, Integer money) {
        String payUrl = "";
        if (money != null && money > 0) {
            payUrl = paypalUtil.pay(money / 100d, GidgabsContants.PAYPAL_BUTLER_ORDER__PRE+ orderCode);
        }
        return ReturnJson.success("支付地址",payUrl);
    }

}
